﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using STOPPLANService.Untity;

namespace STOPPLANService
{
    public partial class STOPPLAN : Form
    {
        public STOPPLAN()
        {
            InitializeComponent();
        }
        public STOPPLANMes mes = new STOPPLANMes();
        public STOPPLANOracle inoracle = new STOPPLANOracle();
        Makelog log = new Makelog();
        //计时器
        private System.Timers.Timer ClockScanDevice;
        private void STOPPLAN_Load(object sender, EventArgs e)
        {
            textBox1.Text = Convert.ToString(ConfigurationManager.AppSettings.GetValues("Intervaltime")[0]);
            textBox2.Text += "测试准备:" + DateTime.Now.ToString() + System.Environment.NewLine;
            Control.CheckForIllegalCrossThreadCalls = false;
            this.ClockScanDevice = new System.Timers.Timer(double.Parse(textBox1.Text)) { AutoReset = false };
            this.ClockScanDevice.Elapsed +=
                (o, args) =>
                {
                    this.ClockScanDevice.Stop();
                    BindConOracle();
                    BindConOracleup();
                    this.ClockScanDevice.Start();
                };
            this.ClockScanDevice.Start();
        }
        /// <summary>
        /// 立即更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //同步
            BindConOracle();
            //出现异常没有第一时间同步，最后重新同步的方法
            BindConOracleup();
        }

        /// <summary>
        /// 同步服务数据库方法
        /// </summary>
        public void BindConOracle()
        {

            DataTable dat = mes.SelectMesSTOPPLAN_View();
            if (dat.Rows.Count > 0)
            {
                for (int i = 0; i < dat.Rows.Count;i++ )
                {
                    STOPPLANS stopmes = new STOPPLANS();
                    if (dat.Rows[i]["MCHID"].ToString().Substring(2,2) == "LH")
                    {
                        
                        if (dat.Rows[i]["FAC"].ToString().Trim()=="01")
                        {
                            
                            stopmes.FACNO = "31";
                        }
                        else if (dat.Rows[i]["FAC"].ToString().Trim() == "02")
                        {
                            stopmes.FACNO = "32";
                        }
                        else if (dat.Rows[i]["FAC"].ToString().Trim() == "03")
                        {
                            stopmes.FACNO = "33";
                        }
                        stopmes.MCHID = dat.Rows[i]["MCHID"].ToString().Trim();
                        stopmes.FAC = dat.Rows[i]["FAC"].ToString().Trim();
                        stopmes.INAM = dat.Rows[i]["INAM"].ToString().Trim();
                        stopmes.ITYPENAM = dat.Rows[i]["ITYPENAM"].ToString().Trim();
                        stopmes.PLANSTIM = dat.Rows[i]["PLANSTIM"].ToString().Trim();
                        stopmes.planUseTime = dat.Rows[i]["planUseTime"].ToString().Trim();
                       int num= inoracle.insertCureBuildingShutDown(stopmes);
                       if (num >0)
                        {
                            textBox2.Text += "硫化信息同步成功！" + System.Environment.NewLine;
                        }
                        else
                        {
                            textBox2.Text += "硫化信息同步失败！" + System.Environment.NewLine;
                        }
                        
                    }
                    else if (dat.Rows[i]["MCHID"].ToString().Substring(2, 2) == "CX")
                    {
                        if (dat.Rows[i]["FAC"].ToString().Trim() == "01")
                        {

                            stopmes.FACNO = "41";
                        }
                        else if (dat.Rows[i]["FAC"].ToString().Trim() == "02")
                        {
                            stopmes.FACNO = "42";
                        }
                        else if (dat.Rows[i]["FAC"].ToString().Trim() == "03")
                        {
                            stopmes.FACNO = "43";
                        }
                        stopmes.MCHID = dat.Rows[i]["MCHID"].ToString().Trim();
                        stopmes.FAC = dat.Rows[i]["FAC"].ToString().Trim();
                        stopmes.INAM = dat.Rows[i]["INAM"].ToString().Trim();
                        stopmes.ITYPENAM = dat.Rows[i]["ITYPENAM"].ToString().Trim();
                        stopmes.PLANSTIM = dat.Rows[i]["PLANSTIM"].ToString().Trim();
                        stopmes.planUseTime = dat.Rows[i]["planUseTime"].ToString().Trim();
                        int num = inoracle.insertCureBuildingShutDown(stopmes);
                        if (num > 0)
                        {
                            textBox2.Text += "成型信息同步成功！" + System.Environment.NewLine;
                        }
                        else
                        {
                            textBox2.Text += "成型信息同步失败！" + System.Environment.NewLine;
                        }
                    }
                    else
                    {
                        stopmes.MCHID = dat.Rows[i]["MCHID"].ToString().Trim();
                        stopmes.FAC = dat.Rows[i]["FAC"].ToString().Trim();
                        stopmes.INAM = dat.Rows[i]["INAM"].ToString().Trim();
                        stopmes.ITYPENAM = dat.Rows[i]["ITYPENAM"].ToString().Trim();
                        stopmes.PLANSTIM = dat.Rows[i]["PLANSTIM"].ToString().Trim();
                        stopmes.planUseTime = dat.Rows[i]["planUseTime"].ToString().Trim();
                        int num = inoracle.insertMaterialShutDown(stopmes);
                        if (num > 0)
                        {
                            textBox2.Text += "半成品信息同步成功！" + System.Environment.NewLine;
                        }
                        else
                        {
                            textBox2.Text += "半成品信息同步失败！" + System.Environment.NewLine;
                        }
                    }
                }
                //A更新时间
                mes.updateMes();
            }
            else
            {
                string bt = "更新服务同步APS";
                string msg = "暂无数据同步APS!";
                log.MakeLog(1,bt,msg);
            }
        }

        /// <summary>
        /// 异常同步方法
        /// </summary>
        public void BindConOracleup()
        {

            DataTable dat = mes.SelectMesSTOPPLAN_Del();
            if (dat.Rows.Count > 0)
            {
                for (int i = 0; i < dat.Rows.Count; i++)
                {
                    STOPPLANS stopmes = new STOPPLANS();
                    if (dat.Rows[i]["MCHID"].ToString().Substring(2, 2) == "LH")
                    {

                        if (dat.Rows[i]["FAC"].ToString().Trim() == "01")
                        {

                            stopmes.FACNO = "31";
                        }
                        else if (dat.Rows[i]["FAC"].ToString().Trim() == "02")
                        {
                            stopmes.FACNO = "32";
                        }
                        else if (dat.Rows[i]["FAC"].ToString().Trim() == "03")
                        {
                            stopmes.FACNO = "33";
                        }
                        stopmes.MCHID = dat.Rows[i]["MCHID"].ToString().Trim();
                        stopmes.FAC = dat.Rows[i]["FAC"].ToString().Trim();
                        stopmes.INAM = dat.Rows[i]["INAM"].ToString().Trim();
                        stopmes.ITYPENAM = dat.Rows[i]["ITYPENAM"].ToString().Trim();
                        stopmes.PLANSTIM = dat.Rows[i]["PLANSTIM"].ToString().Trim();
                        stopmes.planUseTime = dat.Rows[i]["planUseTime"].ToString().Trim();
                        int num = inoracle.insertCureBuildingShutDown(stopmes);
                        if (num > 0)
                        {
                            textBox2.Text += "硫化信息同步成功！" + System.Environment.NewLine;
                        }
                        else
                        {
                            textBox2.Text += "硫化信息同步失败！" + System.Environment.NewLine;
                        }

                    }
                    else if (dat.Rows[i]["MCHID"].ToString().Substring(2, 2) == "CX")
                    {
                        if (dat.Rows[i]["FAC"].ToString().Trim() == "01")
                        {

                            stopmes.FACNO = "41";
                        }
                        else if (dat.Rows[i]["FAC"].ToString().Trim() == "02")
                        {
                            stopmes.FACNO = "42";
                        }
                        else if (dat.Rows[i]["FAC"].ToString().Trim() == "03")
                        {
                            stopmes.FACNO = "43";
                        }
                        stopmes.MCHID = dat.Rows[i]["MCHID"].ToString().Trim();
                        stopmes.FAC = dat.Rows[i]["FAC"].ToString().Trim();
                        stopmes.INAM = dat.Rows[i]["INAM"].ToString().Trim();
                        stopmes.ITYPENAM = dat.Rows[i]["ITYPENAM"].ToString().Trim();
                        stopmes.PLANSTIM = dat.Rows[i]["PLANSTIM"].ToString().Trim();
                        stopmes.planUseTime = dat.Rows[i]["planUseTime"].ToString().Trim();
                        int num = inoracle.insertCureBuildingShutDown(stopmes);
                        if (num > 0)
                        {
                            //删除
                            mes.delDeletetab(dat.Rows[i]["Nonum"].ToString().Trim());
                            textBox2.Text += "成型信息同步成功！" + System.Environment.NewLine;
                        }
                        else
                        {
                            textBox2.Text += "成型信息同步失败！" + System.Environment.NewLine;
                        }
                    }
                    else
                    {
                        stopmes.MCHID = dat.Rows[i]["MCHID"].ToString().Trim();
                        stopmes.FAC = dat.Rows[i]["FAC"].ToString().Trim();
                        stopmes.INAM = dat.Rows[i]["INAM"].ToString().Trim();
                        stopmes.ITYPENAM = dat.Rows[i]["ITYPENAM"].ToString().Trim();
                        stopmes.PLANSTIM = dat.Rows[i]["PLANSTIM"].ToString().Trim();
                        stopmes.planUseTime = dat.Rows[i]["planUseTime"].ToString().Trim();
                        int num = inoracle.insertMaterialShutDown(stopmes);
                        if (num > 0)
                        {
                            //删除
                            mes.delDeletetab(dat.Rows[i]["Nonum"].ToString().Trim());
                            textBox2.Text += "半成品信息同步成功！" + System.Environment.NewLine;
                        }
                        else
                        {
                            textBox2.Text += "半成品信息同步失败！" + System.Environment.NewLine;
                        }
                    }
                }
               
            }
            else
            {
                string bt = "更新服务同步APS";
                string msg = "暂无数据同步APS!";
                log.MakeLog(1, bt, msg);
            }
        }
    }
}
